package com.hemerasolutions.v2trainer.converter;

import java.io.Serializable;
import java.util.Map;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hemerasolutions.v2trainer.dao.interfaces.TrainingTypeDaoInterface;
import com.hemerasolutions.v2trainer.model.interfaces.BaseModelInterface;

@Component("imzConverter")
@Scope("request")
public class IntensityModZoneConverter implements Converter, Serializable {

	private static final long serialVersionUID = 1L;
	
	@Autowired
	private TrainingTypeDaoInterface ttDao;

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent component, String value) {
		
		 if (value != null) {  
	            return this.getAttributesFrom(component).get(value);  
	        }  
	        return null;  
	}

	@Override
    public String getAsString(FacesContext ctx, UIComponent component, Object value) {  
		  
        if (value != null   && !"".equals(value)) {  
  
        	BaseModelInterface entity = (BaseModelInterface) value;  
  
            // adiciona item como atributo do componente  
            this.addAttribute(component, entity);  
  
            int codigo = entity.getId();  
            return String.valueOf(codigo);  
        }  
  
        return (String) value;  
    }  

    protected void addAttribute(UIComponent component, BaseModelInterface o) {  
        String key = o.getId().toString(); 
        this.getAttributesFrom(component).put(key, o);  
    }  
  
    protected Map<String, Object> getAttributesFrom(UIComponent component) {  
        return component.getAttributes();  
    }  	
	

}

